package com.zhss.concurrency;


import sun.misc.Unsafe;

import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;
import java.util.stream.IntStream;

/**
 * @ClassName LongAddrDemo
 * @Description TODO
 * @Author leejiliang
 * @DateTime 2021/6/21 16:36
 * @Version 1.0
 */
public class LongAddrDemo {
//	private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
//	private static final long VALUE = U.objectFieldOffset(AtomicLong.class, "value");
	static long count=0;

	public void unsafeAdd() {
		count++;
	}

	public void safeSlowAdd(){
		synchronized (this){
			count++;
		}
	}

	public void atomicLongAdd() {
		AtomicLong atomicLong = new AtomicLong(0);
		final long l = atomicLong.incrementAndGet();
	}
	private volatile long vcount=0;

	public long getVcount() {
		return vcount;
	}

	public void zhuangBiAdd() {
		final Unsafe unsafe = Unsafe.getUnsafe();
		boolean success=false;
		long v = vcount;
		while (!success) {
//			success=unsafe.compareAndSwapLong(
//					LongAdder.class, VALUE, v, v + 1);
		}
	}

	public static void main(String[] args) throws InterruptedException {
		final LongAddrDemo longAddrDemo = new LongAddrDemo();
		IntStream.range(0,10).forEach(i->{
			new Thread(()-> longAddrDemo.zhuangBiAdd()).start();
		});
		Thread.sleep(1000);
		System.out.println(longAddrDemo.getVcount());
	}
}
